package com.pinyoumei.app.view.LuckPan;

/**
 * Created by Administrator on 2021-09-04.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-09-04        Administrator             新增Person类
 * 2021-09-04        Administrator             增加sex属性
 */

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;

import com.pinyoumei.app.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多宫格抽奖(9宫格以上(3*3)抽奖)
 */
public class MultiMiyagiRaffle<T> {
    public static int LINE=4;   //行
    public static int COLUMN=  3  ;//列



    List<T> list;//抽奖的数据列表

    //  private Integer[]=new Integer[]{0,1,2,3,7,11,15,14,13,12,8,4};
    private List<Integer> luckPosition = new ArrayList<>();


    public Map<Integer, T> luckMap = new HashMap<>();//抽奖位置和RecyclerView位置对应关系

    //动画运行后，选中的item的变化
    private StatusChange statusChange;


    private void initLuckMap() {
        for (int i = 0; i < luckPosition.size(); i++) {
            luckMap.put(luckPosition.get(i), list.get(i));
        }
    }

    public MultiMiyagiRaffle( List<T> list, StatusChange statusChange) {

        this.list = list;
        this.statusChange = statusChange;

        getluckPosition();
        initLuckMap();
    }


    public void getluckPosition() {

        luckPosition.clear();
        //运动顺序为顺时针--按运动顺序加入所有边缘的元素
        //1.先加入第一行的所有元素
        for (int column = 0; column < COLUMN; column++) {
            luckPosition.add(column);

        }

        //2.加入最后一列的(最后一列的第一行已经加入所以从1开始)
        for (int line = 1; line < LINE; line++) {
            luckPosition.add(COLUMN * line + (COLUMN - 1));
        }
        //3.先加入最后一行的所有元素(最后一行的最后一列已经加入所以从1开始)
        for (int column = 1; column < COLUMN; column++) {
            luckPosition.add((COLUMN * LINE - 1) - column);

        }

        //4.先加入第一列的所有元素(最后一行的第一行已经加入所以从1开始)

        for (int line=LINE-1-1;line>0;line--){
            luckPosition.add(COLUMN * line);
        }

        LogUtils.i("luckPosition", luckPosition.toString());
    }


    private boolean mShouldStartNextTurn = true; // 标记是否应该开启下一轮抽奖

    public void startAnim() {
        if (!mShouldStartNextTurn) {
            return;
        }
//        Random random = new Random();
//        setLuckNum(random.nextInt(luckMap.size() - 1)); // 生成 [0,11) 的随机整数
        //mLuckNum=2;
        LogUtils.e("setCurrentPosition--mLuckNum=", mLuckNum + "");

//        ToastUtils.showShort("预设结果==" + mLuckNum);

        ValueAnimator animator = ValueAnimator.ofInt(mStartLuckPosition, mRepeatCount * luckMap.size() + mLuckNum)
                .setDuration(6000);
        animator.setInterpolator( new EaseCubicInterpolator(0.3f, 0.23f,0.2f, 0.9f));//自定义插值器  https://www.cnblogs.com/qiuqiuQaQ/p/11912569.html我的另外一篇文章（转载）


        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                final int position = (int) animation.getAnimatedValue();

                LogUtils.e("setCurrentPosition==ValueAnimator==", animation.getAnimatedValue() + "");
                setCurrentPosition(position % luckMap.size());
                mShouldStartNextTurn = false;
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mShouldStartNextTurn = true;
                mStartLuckPosition = mLuckNum;
                //最终选中的位置
                if (mLuckAnimationEndListener != null) {
                    mLuckAnimationEndListener.onLuckAnimationEnd(mLuckNum,
                            "mStartLuckPosition==" + mStartLuckPosition);
                }
            }
        });

        animator.start();
    }


    /**
     * 可以通过对 mLuckNum 设置计算策略，来控制用户 中哪些奖 以及 中大奖 的概率
     */
    private int mLuckNum = 3; //最终中奖位置(这个值是多少就停在哪个位置)

    public void setLuckNum(int luckNum) {
        mLuckNum = luckNum;
    }

    private int mRepeatCount = 3; // 转的圈数

    private int mStartLuckPosition = 0; // 开始抽奖的位置
    private int mCurrentPosition = -1; // 当前转圈所在的位置

    /**
     * 用于抽奖结果回调
     */
    public interface OnLuckAnimationEndListener {
        void onLuckAnimationEnd(int pos, String msg);
    }

    private OnLuckAnimationEndListener mLuckAnimationEndListener;

    public void setLuckAnimationEndListener(OnLuckAnimationEndListener luckAnimationEndListener) {
        mLuckAnimationEndListener = luckAnimationEndListener;
    }

    private void setCurrentPosition(int position) {

        if (mCurrentPosition == position) {
            return;
        }
        //   log("setCurrentPosition==",position+"");
        if (mCurrentPosition != -1) {
            int truePosition = luckPosition.get(mCurrentPosition);
            // log("setCurrentPosition---last--truePosition",truePosition+"");
            statusChange.changeClose(truePosition, false);
        }

        int truePosition = luckPosition.get(position);

        mCurrentPosition = position;

        //log("setCurrentPosition---new--truePosition",truePosition+"");
        statusChange.changeClose(truePosition, true);
    }


    public interface StatusChange {
        void changeClose(int position, boolean ischeck);
    }


}

